﻿using FlexFlow.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlexFlow.Runtime
{
    /// <summary>
    /// 表示工作流系统机构角色
    /// </summary>
    public static class SystemOrganizations
    {
        /// <summary>
        /// 表示系统管理员角色
        /// </summary>
        public static Organization Administrator { get; private set; }

        static SystemOrganizations()
        {
            Administrator = new Organization()
            {
                Name = "系统管理员",
                OrganizationType = "系统管理",
                Identity = Guid.Parse("12F79EA7-5326-40B0-8D48-A1C46A467FFF")
            };
        }
    }
    /// <summary>
    /// 表示组织机构管理器
    /// </summary>
    public class OrganizeManager : ManagerBase
    {
        /// <summary>
        /// 获取或设置引擎默认的组织机构管理器
        /// </summary>
        public static OrganizeManager Default { get; set; }

        /// <summary>
        /// 创建一个组织机构
        /// </summary>
        /// <param name="parentOrg">
        /// 该组织机构的父级组织机构
        /// </param>
        /// <param name="name">
        /// 该组织机构的名称
        /// </param>
        /// <returns>
        /// 创建的组织机构
        /// </returns>
        public OperateResult Create(string orgType, string name, Guid? parentOrg = null)
        {
            //检测组织机构类型
            if (orgType != "部门" && orgType != "单位" && orgType != "岗位")
            {
                return new OperateResult()
                {
                    ResultCode = 0x020101,
                    Message = "组织机构类型必须为部门，单位或岗位！"
                };
            }

            //添加岗位
            Organization org = this.Components.OrganizeProvider.Create();
            org.OrganizationType = orgType;
            org.ParentOrganization = parentOrg;
            org.Name = name;

            if (!this.Components.OrganizeProvider.Add(org))
            {
                return new OperateResult()
                {
                    ResultCode = 0x020102,
                    Message = "数据操作失败！"
                };
            }

            return OperateResult.SuccessResult(org);
        }

        /// <summary>
        /// 取得组织机构列表
        /// </summary>
        /// <param name="parentOrg">
        /// 要取得列表的父级组织机构
        /// </param>
        /// <returns>
        /// 取得的组织机构列表
        /// </returns>
        public OperateResult GetList(Guid? parentOrg = null)
        {
            List<Organization> res = this.Components.OrganizeProvider.GetList(parentOrg);
            return OperateResult.SuccessResult(res);
        }

        /// <summary>
        /// 更新指定的组织机构实体
        /// </summary>
        /// <param name="org">
        /// 要更新的组织机构实体
        /// </param>
        /// <returns>
        /// 更新结果
        /// </returns>
        public OperateResult Update(Organization org)
        {
            //尝试更新
            if (!this.Components.OrganizeProvider.Update(org))
            {
                return new OperateResult()
                {
                    ResultCode = 0x020201,
                    Message = "组织机构更新失败！可能组织机构已被移除！"
                };
            }

            return OperateResult.SuccessResult();
        }

        /// <summary>
        /// 删除指定的组织机构及其所有下属机构
        /// </summary>
        /// <param name="org">
        /// 要删除的组织机构的Id
        /// </param>
        /// <returns>
        /// 删除结果
        /// </returns>
        public OperateResult Delete(Guid org)
        {
            //取得所有下属机构
            OperateResult res = GetList(org);
            //失败
            if (res.ResultCode != 200)
            {
                return new OperateResult()
                {
                    ResultCode = 0x020301,
                    Message = "取得下属组织机构失败！",
                    Tag = res
                };
            }
            else
            {
                foreach (var item in (List<Organization>)res.Tag)
                {
                    OperateResult subDelRes = Delete(item.Identity);
                    if (subDelRes.ResultCode != 200)
                    {
                        return new OperateResult()
                        {
                            ResultCode = 0x020302,
                            Message = "删除下属组织失败！",
                            Tag = subDelRes
                        };
                    }
                }
                //尝试删除
                if (!this.Components.OrganizeProvider.Delete(org))
                {
                    //删除失败
                    return new OperateResult()
                    {
                        ResultCode = 0x020303,
                        Message = "删除组织实体失败！"
                    };
                }

                //返回结果
                return OperateResult.SuccessResult(org);
            }
        }

        /// <summary>
        /// 重命名指定组织机构
        /// </summary>
        /// <param name="orgId">
        /// 要重命名的组织机构Id
        /// </param>
        /// <param name="newName">
        /// 要重命名的组织机构的新名称
        /// </param>
        /// <returns>
        /// 一个表示是否重命名成功的值
        /// </returns>
        public OperateResult Rename(Guid org, string newName)
        {
            //重命名
            bool res = this.Components.OrganizeProvider.Rename(org, newName);
            if (!res)
            {
                return new OperateResult()
                {
                    ResultCode = 0x020401,
                    Message = "重命名失败！"
                };
            }

            return OperateResult.SuccessResult();
        }

        /// <summary>
        /// 重新设定指定组织机构的父组织机构
        /// </summary>
        /// <param name="org">
        /// 要设定的组织机构
        /// </param>
        /// <param name="parGuid">
        /// 要设定的组织机构的父组织机构
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public OperateResult SetParent(Guid org, Guid? parGuid)
        {
            if (parGuid != null && !this.Components.OrganizeProvider.Exist((Guid)parGuid))
            {
                return new OperateResult()
                {
                    ResultCode = 0x020501,
                    Message = "指定的父组织机构不存在！"
                };
            }
            bool res = this.Components.OrganizeProvider.SetParent(org, parGuid);
            if (!res)
            {
                return new OperateResult()
                {
                    ResultCode = 0x020502,
                    Message = "设置失败！"
                };
            }
            else
            {
                return OperateResult.SuccessResult();
            }
        }
        /// <summary>
        /// 取得具有指定ID的组织机构实体
        /// </summary>
        /// <param name="orgId">
        /// 要取得实体的组织机构ID
        /// </param>
        /// <returns>
        /// 取得的组织机构实体
        /// </returns>
        public OperateResult Get(Guid orgId)
        {
            Organization org = this.Components.OrganizeProvider.Get(orgId);
            if (org == null)
            {
                return new OperateResult()
                {
                    ResultCode = 0x020601,
                    Message = "指定组织机构已删除或不存在！"
                };
            }

            return OperateResult.SuccessResult(org);
        }
        /// <summary>
        /// 更新指定组织机构的信息
        /// </summary>
        /// <param name="orgId">
        /// 组织机构ID
        /// </param>
        /// <param name="name">
        /// 组织机构名称
        /// </param>
        /// <param name="type">
        /// 组织机构类型
        /// </param>
        /// <param name="leaders">
        /// 组织机构领导
        /// </param>
        /// <param name="chargeLeaders">
        /// 组织机构分管领导
        /// </param>
        /// <param name="locked">
        /// 组织机构是否被锁定
        /// </param>
        /// <returns>
        /// 更新结果
        /// </returns>
        public OperateResult Update(Guid orgId, string name = null, string type = null, string leaders = null, string chargeLeaders = null, bool? locked = null)
        {
            if (!this.Components.OrganizeProvider.Update(orgId, name, type, leaders, chargeLeaders, locked))
            {
                return new OperateResult()
                {
                    ResultCode = 0x020701,
                    Message = "数据更新失败！"
                };
            }

            return OperateResult.SuccessResult();
        }
        /// <summary>
        /// 查询名称中包含指定字符的组织机构集合
        /// </summary>
        /// <param name="nameLike">
        /// 要查询的组织机构名称
        /// </param>
        /// <param name="limit">
        /// 限制数量
        /// </param>
        /// <param name="skip">
        /// 返回数据的起始位置
        /// </param>
        /// <param name="isAll">
        /// 一个值，表示返回的结果是所有的表单模板还是已安装的表单模板
        /// </param>
        /// <returns>
        /// 查询结果
        /// </returns>
        public OperateResult QueryByName(string nameLike, int skip = 0, int limit = 5)
        {
            if (limit > 50 || limit <= 0)
            {
                return new OperateResult()
                {
                    ResultCode = 0x020801,
                    Message = "limit必须在1-50之间！"
                };
            }

            return OperateResult.SuccessResult(this.Components.OrganizeProvider.QueryByName(nameLike, skip, limit));
        }
        /// <summary>
        /// 初始或默认值
        /// </summary>
        static OrganizeManager()
        {
            Default = new OrganizeManager();
        }
    }
}
